var addHandler = function(item, evt, treePanelObj){
	var selNode;
	selNode = treePanelObj.getSelectionModel().getSelectedNode();

	if (selNode) {
		createAddForm(selNode, treePanelObj);
	}
}

var editHandler = function(item, evt, treePanelObj){
	var selNode;
	selNode = treePanelObj.getSelectionModel().getSelectedNode();
	if (selNode) {
		createEditForm(selNode, treePanelObj);
	}
}
	
var deleteHandler = function(item, evt, treePanelObj){
	var selNode;
	selNode = treePanelObj.getSelectionModel().getSelectedNode();
		
	if (selNode) {
		Ext.MessageBox.confirm(
				'Are you sure?',
				'Please confirm the deletion of ' + selNode.text,
				function(btn) {
					onConfirmDelete(btn, treePanelObj);
				}
		)
	}
}

var refreshHandler = function(item, evt, treePanelObj){
	var selNode;
	selNode = treePanelObj.getSelectionModel().getSelectedNode();
	if (selNode) {
		onRefresh(selNode, treePanelObj);
	}
}

var addItem = function(treePanelObj) {
	return new Ext.menu.Item({
		itemId  : 'add',
    	iconCls : 'add',
    	handler : function(item, evt) {
    		addHandler(item, evt, treePanelObj);
    	}
	});
}

var editItem = function(treePanelObj) {
	return new Ext.menu.Item({
		itemId  : 'edit',
    	iconCls : 'edit',
    	handler : function(item, evt) {
    		editHandler(item, evt, treePanelObj);
    	}
	});
}

var deleteItem = function(treePanelObj) {
	return new Ext.menu.Item({
		itemId  : 'delete',
    	iconCls : 'delete',
    	handler : function(item, evt) {
    		deleteHandler(item, evt, treePanelObj);
    	}
	});
}

var refreshItem = function(treePanelObj) {
	return new Ext.menu.Item({
		itemId  : 'refresh',
    	iconCls : 'refresh',
    	handler : function(item, evt) {
    		refreshHandler(item, evt, treePanelObj);
    	}
	});
}

var buildContextMenu = function(node, treePanelObj) {
	var menu = new Ext.menu.Menu();
	
	var _add     = addItem(treePanelObj);
	var _edit    = editItem(treePanelObj);
	var _delete  = deleteItem(treePanelObj);
	var _refresh = refreshItem(treePanelObj);
		
	if (node.id =='_treeRoot_') {
		_add.setText('Add Department');
		_refresh.setText('Refresh');
			
		menu.addItem(_add);
		menu.addItem('-');
		menu.addItem(_refresh);
	}else if (node.id.match('_DEP_')) {
		_add.setText('Add Project');
		_edit.setText('Edit Department');
		_delete.setText('Delete Department');

		menu.addItem(_add);
		menu.addItem('-');
		menu.addItem(_edit);
		menu.addItem('-');
		menu.addItem(_delete);
	}
	else if (node.id.match('_PRJ_')) {
		_add.setText('Add Employee');
		_edit.setText('Edit Project');
		_delete.setText('Delete Project');

		menu.addItem(_add);
		menu.addItem('-');
		menu.addItem(_edit);
		menu.addItem('-');
		menu.addItem(_delete);
	}
	else if (node.leaf) {		
		_edit.setText('Edit Employee');
		_delete.setText('Delete Employee');

		menu.addItem(_edit);
		menu.addItem('-');
		menu.addItem(_delete);
	}
	
	return menu;
}

var onContextMenu = function(node, evt, treePanelObj) {
	node.select();
	evt.stopEvent();
	
	var ctxMenu = buildContextMenu(node, treePanelObj);

	ctxMenu.showAt(evt.getXY()) ;
}